<template>
  <div class="app-container">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="150px"
      class="demo-ruleForm"
      style="width: 800px;"
    >
      <el-form-item label="姓名" prop="teacherName">
        <el-input v-model="ruleForm.teacherName" />
      </el-form-item>
      <el-form-item label="照片" prop="photo">
        <my-upload v-model="ruleForm.photo" :limit="1" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNumber">
        <el-input v-model="ruleForm.idCardNumber" />
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-radio-group v-model="ruleForm.education">
          <el-radio v-for="item in educationOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="所在学校" prop="subSchoolId">
        <el-radio-group v-model="ruleForm.subSchoolId">
          <el-radio v-for="item in schoolOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="ruleForm.mobile" />
      </el-form-item>
      <el-form-item label="单位负责人" prop="unitLeader">
        <el-input v-model="ruleForm.unitLeader" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'
import MyUpload from '@/components/my-upload/index'
import { addTeacher, getTeacherDetail, getXSchoolList } from '@/api/basic'
import { educationOptions } from '../../attr'
export default {
  components: { MyUpload },
  data() {
    return {
      schoolOptions: [],
      educationOptions,
      ruleForm: {},
      rules: {
        teacherName: [
          { required: true, message: '请输入教师姓名', trigger: 'blur' },
          { max: 20, message: '教师姓名不超过20个字符', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择教师性别', trigger: 'change' }
        ],
        idCardNumber: [
          { required: true, message: '请输入教师身份证号', trigger: 'blur' },
          {
            max: 18,
            min: 18,
            message: '请输入正确的18位身份证号',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, pattern: /^1[23456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        education: [
          { required: true, message: '请选择学历', trigger: 'change' }
        ],
        subSchoolId: [
          { required: true, message: '请选择所在学校', trigger: 'change' }
        ],
        unitLeader: [
          { required: true, message: '请输入单位负责人', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    const route = this.$route
    this.getXSchool()
    if (route.name === 'teacher-edit') {
      this.getDetail(route.params.id)
    }
  },
  methods: {
    async getDetail(id) {
      try {
        const res = await getTeacherDetail({
          id: Number(id)
        })
        if (res) {
          const result = JSON.parse(JSON.stringify(res))
          if (res.photo) {
            result.photo = [{
              url: res.photo
            }]
          } else {
            result.photo = []
          }
          this.ruleForm = result
        }
      } catch (err) {
        console.log('接口出错')
      }
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(that.ruleForm))
          params.birthday = moment(that.ruleForm.birthday).format('YYYY-MM-DD')
          params.joinWorkTime = moment(that.ruleForm.joinWorkTime).format(
            'YYYY-MM-DD'
          )
          params.graduationDate = moment(that.ruleForm.graduationDate).format(
            'YYYY-MM'
          )
          if (that.ruleForm.photo) {
            params.photo = that.ruleForm.photo[0].url
          }
          addTeacher(params).then((res) => {
            if (res) {
              that.$message({
                message: `${this.$route.name === 'teacher-edit' ? '编辑' : '新增'}教师成功！`,
                type: 'success'
              })
              that.$router.push('/basic/teacher')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async getXSchool() {
      try {
        const res = await getXSchoolList({})
        if (res) {
          this.schoolOptions = res.map(v => ({
            label: v.schoolName,
            value: v.id
          }))
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
<style scoped lang="less">
ul,
li {
  padding: 0;
  margin: 0;
}
li {
  list-style: none;
}
.app-container {
  background: #fff;
  flex: 1;
}
.match-type-check-area {
  margin-top: 12px;
  .left {
    display: block;
    margin-bottom: 5px;
  }
}
</style>
